package net.dgg.rz.production.controller;

import com.alibaba.fastjson.JSONArray;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.StringUtils;
import net.dgg.rz.api.production.service.ApiProductionService;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.service.CommonService;
import net.dgg.rz.common.utils.DataTableResponse;
import net.dgg.rz.common.utils.ExcelTool;
import net.dgg.rz.production.entity.ProductNodeFlow;
import net.dgg.rz.production.entity.ProductOrder;
import net.dgg.rz.production.entity.dto.ProductOrderDto;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.FlowNodeService;
import net.dgg.rz.production.service.ProductOrderService;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className AuditOrderController
 * @Author Quanlin Wan
 * @Description //TODO 待审核的生产订单-控制层
 * @Date 2018/12/19 13:41
 **/
@Controller
@RequestMapping(value = "/rzsc/audit_order")
public class AuditOrderController extends DggBaseController {

    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private FlowNodeService flowNodeService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private ApiProductionService apiProductionService;
    /**
     * @Author Quanlin Wan
     * @Description //TODO 待审核页面
     * @Date 16:25 2018/12/19
     * @Param
     * @return
     **/
    @RequestMapping("/index.html")
    public String index(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/auditorder/audit_order";
    }

    /**
     * @Author sdx
     * @Description //TODO 完结待审核页面
     * @Date 2020-11-4 14:44:28
     * @Param
     * @return
     **/
    @RequestMapping("/completion_review_page.html")
    public String completionReview(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/auditorder/completion_review_page";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 退单待审核页面
     * @Date 11:26 2018/12/24
     * @Param
     * @return
     **/
    @RequestMapping("/abandon_page.html")
    public String abandonPage(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/auditorder/abandon_page";
    }
    /**
     * @Author Quanlin Wan
     * @Description //TODO 废单待审核页面
     * @Date 11:26 2018/12/24
     * @Param
     * @return
     **/
    @RequestMapping("/nullify_page.html")
    public String nullifyPage(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/auditorder/nullify_page";
    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO 待审核分页数据
     * @Date 11:26 2018/12/24
     * @Param
     * @return
     **/


    @RequestMapping("/list.do")
    @ResponseBody
    public DggRestResponse list(@RequestParam Map params) {
        try {
            List<ProductOrderDto> productOrderDtos = productOrderService.auditOrderWithPage(params);
            return new DataTableResponse().data(params, productOrderDtos);
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }
    /**
     * @Author sdx
     * @Description //TODO 完结审核弹窗
     * @Date 2020-11-5 16:10:38
     * @Param
     * @return
     **/
    @RequestMapping("/audit_order_dialog.html")
    public String auditOrderDialog(Model model,String ids){
        try {
            JSONArray jsonArray = JSONArray.parseArray(ids);
            List<Long> idArray = jsonArray.toJavaList(Long.class);
            List<ProductOrder> orderList = null;
            if(idArray.size() == 1){
                model.addAttribute("id", idArray.get(0));
            }
            if(idArray.size() > 1){
                orderList= productOrderService.selectByOrderIds(idArray);
            }
            model.addAttribute("orderList", orderList);
            return "rzsc/auditorder/audit_order_dialog";
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 退单审核
     * @Date 11:22 2018/12/28
     * @Param
     * @return
     **/

    @RequestMapping("/audit_order.do")
    @ResponseBody
    public DggRestResponse auditOrder(String params){
        try {
            productOrderService.auditOrder(params);
            return this.getSuccessResponse("操作成功!");
        } catch (CommonExcption e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 退单审核弹窗
     * @Date 16:23 2018/12/27
     * @Param 
     * @return 
     **/
    @RequestMapping("/abandon_audit_dialog.html")
    public String abandonAuditDialog(Model model,String id){
        try {
            ProductOrder productOrder= productOrderService.selectByPrimaryKey(Long.valueOf(id));
            Map map = new HashMap();
            ProductNodeFlow nodeFlow = null;
            Long serialId = commonService.getSerialId(productOrder.getProductId());
            if(net.dgg.rz.common.utils.StringUtils.isNotEmpty(productOrder.getNodeJson())){
                map.put("productId", serialId);
                map.put("businessTypeCode", productOrder.getBusinessTypeCode());//一级业态Code
                map.put("smallBusinessTypeCode", productOrder.getBusinessProductCode());// 二级业态Code
                nodeFlow = apiProductionService.getNodeFlow(map);
            }else{
                map.put("flowId", productOrder.getNodeFlowId());
                nodeFlow = flowNodeService.findFlowById(map);
            }
            if(null == nodeFlow.getExamineGrade()){
                nodeFlow.setExamineGrade(1);
            }
            //翻译字段
            //String[] transColum = {"originCode", "status", "abandonReason"};
            String[] transColum = {"originCode", "status"};
            productOrder = translateUtil.translateObject(transColum, productOrder);
            model.addAttribute("productNodeFlow", nodeFlow);
            model.addAttribute("productOrder", productOrder);
            return "rzsc/auditorder/abandon_dialog";
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 退单审核
     * @Date 11:22 2018/12/28
     * @Param
     * @return
     **/

    @RequestMapping("/abandon_audit.do")
    @ResponseBody
    public DggRestResponse abandonAudit(String params){
        try {
            productOrderService.abandonAudit(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 批量审核页面
     * @Date 16:52 2019/4/25
     * @Param
     * @return
     **/
    @RequestMapping("/mulity_dialog.html")
    public String mulityPage(Model model,String ids){
        JSONArray jsonArray = JSONArray.parseArray(ids);
        model.addAttribute("size", jsonArray.size());
        return "rzsc/auditorder/mulity_dialog";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 退单批量审核
     * @Date 16:33 2019/4/25
     * @Param
     * @return
     **/
    @RequestMapping("/abandon_audit_mulity.do")
    @ResponseBody
    public DggRestResponse abandonAuditMulity(String params){
        try {
            productOrderService.abandonAuditMulity(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }



    /**
     * @Author Quanlin Wan
     * @Description //TODO 废单审核弹窗
     * @Date 16:23 2018/12/27
     * @Param
     * @return
     **/
    @RequestMapping("/nullify_audit_dialog.html")
    public String nullifyAuditDialog(Model model,String id){
        try {
            ProductOrder productOrder= productOrderService.selectByPrimaryKey(Long.valueOf(id));
            Map map = new HashMap();
            ProductNodeFlow nodeFlow = null;
            Long serialId = commonService.getSerialId(productOrder.getProductId());
            if(net.dgg.rz.common.utils.StringUtils.isNotEmpty(productOrder.getNodeJson())){
                map.put("productId", serialId);
                map.put("businessTypeCode", productOrder.getBusinessTypeCode());//一级业态Code
                map.put("smallBusinessTypeCode", productOrder.getBusinessProductCode());// 二级业态Code
                nodeFlow = apiProductionService.getNodeFlow(map);
            }else{
                map.put("flowId", productOrder.getNodeFlowId());
                nodeFlow = flowNodeService.findFlowById(map);
            }
            if(null == nodeFlow.getExamineGrade()){
                nodeFlow.setExamineGrade(1);
            }
            //翻译字段
            //String[] transColum = {"originCode", "status", "abandonReason"};
            String[] transColum = {"originCode", "status"};
            productOrder = translateUtil.translateObject(transColum, productOrder);
            model.addAttribute("productNodeFlow", nodeFlow);
            model.addAttribute("productOrder", productOrder);
            return "rzsc/auditorder/nullify_dialog";
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }
    /**
     * @Author Quanlin Wan
     * @Description //TODO 废单审核
     * @Date 11:22 2018/12/28
     * @Param
     * @return
     **/
    @RequestMapping("/nullify_audit.do")
    @ResponseBody
    public DggRestResponse nullifyAudit(String params){
        try {
            productOrderService.nullifyAudit(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 退单批量审核
     * @Date 16:33 2019/4/25
     * @Param
     * @return
     **/
    @RequestMapping("/nullify_audit_mulity.do")
    @ResponseBody
    public DggRestResponse nullifyAuditMulity(String params){
        try {
            productOrderService.nullifyAuditMulity(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 撤回预处理(判断操作权限）
     * @Date 11:22 2018/12/28
     * @Param
     * @return
     **/
    @RequestMapping("/pre_withdrawn.do")
    @ResponseBody
    public DggRestResponse preWithdrawn(String params){
        try {
            productOrderService.preWithdrawn(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }
    /**
     * @Author Quanlin Wan
     * @Description //TODO 退单、废单审核撤回
     * @Date 11:22 2018/12/28
     * @Param
     * @return
     **/
    @RequestMapping("/withdrawn.do")
    @ResponseBody
    public DggRestResponse withdrawn(String params){
        try {
            productOrderService.withdrawn(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO 变更审核人弹窗
     * @Date 16:23 2018/12/27
     * @Param
     * @return
     **/
    @RequestMapping("/change_auditor_dialog.html")
    public String changAuditorDialog(Model model,String id,String objId){
        try {
            if(StringUtils.isNotEmpty(id)){
                ProductOrder productOrder = productOrderService.selectByPrimaryKey(Long.valueOf(id));
                //翻译字段
                String[] transColum = {"originCode", "status", "abandonReason"};
                productOrder = translateUtil.translateObject(transColum, productOrder);
                model.addAttribute("productOrder", productOrder);
            }
            if(StringUtils.isNotEmpty(objId)){
                model.addAttribute("objId", objId);
            }
            return "rzsc/auditorder/change_auditor_dialog";
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO 变更审核人
     * @Date 11:22 2018/12/28
     * @Param
     * @return
     **/
    @RequestMapping("/chang_auditor.do")
    @ResponseBody
    public DggRestResponse changAuditor(String params){
        try {
            productOrderService.changAuditor(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProductOrderExcption("操作失败");
        }
    }



    /**
     * 导出(退单待审核)
     */
    @RequestMapping(value = "/excel_export_nullify_audit.do")
    @ResponseBody
    public void exportDetailExcelAll(@RequestParam Map params) {
        int[] widths = new int[] { 512*8,512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8,612*8};
        int[] styleArr = new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        List<ProductOrderDto> list = productOrderService.auditOrderWithPage(params);
        String target = params.get("target").toString();
        String sheetName = new String();
        String[] title = null;
        String[] keyArray = null;
        switch (target) {
            case "0":
                sheetName  = "退单待审核生产订单数据";
                title = new String[]{"所属生产事业部","生产订单编号","订单编号", "客户名称","客户电话","申请退单时间","退单原因","当前审核人","服务合同编号","签单时间","接单时间","签单人","流程人员","业务类型","产品","渠道","支行","金额"};
                keyArray = new String[] {"productOrgName","scProductOrderNo","orderNo","customerName","customerPhone","abandonReasonTime","abandonReason","auditorName","contractNo","orderTime","receiveOrderDate","signUserName","flowUserName","businessProductName","productName","channelName","subBranch","loanAmount"};
                ExcelTool.execlExport(list,sheetName,title,keyArray,widths,styleArr);
                break;
            case "1":
                sheetName  = "废单待审核生产订单数据";
                title = new String[]{"所属生产事业部","生产订单编号","订单编号", "客户名称","客户电话","申请废单时间","废单原因","当前审核人","服务合同编号","签单时间","接单时间","签单人","流程人员","业务类型","产品","渠道","支行","金额"};
                keyArray = new String[] {"productOrgName","scProductOrderNo","orderNo","customerName","customerPhone","abandonReasonTime","abandonReason","auditorName","contractNo","orderTime","receiveOrderDate","signUserName","flowUserName","businessProductName","productName","channelName","subBranch","loanAmount"};
                ExcelTool.execlExport(list,sheetName,title,keyArray,widths,styleArr);
                break;
            default:
                sheetName  = "退单待审核生产订单数据";
                title = new String[]{"所属生产事业部","生产订单编号","订单编号", "客户名称","客户电话","申请退单时间","退单原因","当前审核人","服务合同编号","签单时间","接单时间","签单人","流程人员","业务类型","产品","渠道","支行","金额"};
                keyArray = new String[] {"productOrgName","scProductOrderNo","orderNo","customerName","customerPhone","abandonReasonTime","abandonReason","auditorName","contractNo","orderTime","receiveOrderDate","signUserName","flowUserName","businessProductName","productName","channelName","subBranch","loanAmount"};
                ExcelTool.execlExport(list,sheetName,title,keyArray,widths,styleArr);
                break;
        }

    }

}
